import { Node, Tween, Vec2, Vec3, find, instantiate, tween, v3 } from "cc";
import { ResourceUtil } from "../Utils/ResourceUtil";
import { ZTool } from "../Utils/ZTool";

export default class GuideManager {

    static nodes: Node[] = [];

    public static MoveByLine(lines: Node[], timeGap: number = 0.5) {
        if (!lines) return;

        switch (lines.length) {
            case 2:
                GuideManager.MoveByPoints2(lines, timeGap);
                break;
            case 3:
                GuideManager.MoveByPoints3(lines, timeGap);
                break;
            case 4:
                GuideManager.MoveByPoints4(lines, timeGap);
                break;
            default:
                if (lines.length > 4) {
                    console.error("节点长度超过4");
                    lines = [lines[0], lines[1], lines[2], lines[3]];
                    GuideManager.MoveByPoints4(lines, timeGap);
                }
                break;
        }
    }

    private static MoveByPoints2(lines: Node[], timeGap: number = 0.5) {
        ResourceUtil.LoadPrefab("小手").then((prefab) => {
            let node = instantiate(prefab) as Node;
            GuideManager.nodes.push(node);
            node.setParent(find("Canvas"));

            let positions = [];
            lines.forEach(e => { positions.push(); });
            node.setPosition(positions[0]);

            let t = tween(node)
                .to(timeGap, { position: v3(positions[1]) })
                .call(() => { node.setPosition(positions[0]); })
                .delay(timeGap);

            tween(node).repeatForever(t).start();
        });
    }

    private static MoveByPoints3(lines: Node[], timeGap: number = 0.5) {
        ResourceUtil.LoadPrefab("小手").then((prefab) => {
            let node = instantiate(prefab) as Node;
            GuideManager.nodes.push(node);
            node.setParent(find("Canvas"));

            let positions = [];
            lines.forEach(e => { positions.push(); });
            node.setPosition(positions[0]);

            let timerGap2 = Vec3.distance(positions[1], positions[2]) / Vec3.distance(positions[0], positions[1]) * timeGap;
            let t = tween(node)
                .to(timeGap, { position: v3(positions[1]) })
                .to(timerGap2, { position: v3(positions[2]) })
                .call(() => { node.setPosition(positions[0]); })
                .delay(timeGap);

            tween(node).repeatForever(t).start();
        });
    }

    private static MoveByPoints4(lines: Node[], timeGap: number = 0.5) {
        ResourceUtil.LoadPrefab("小手").then((prefab) => {
            let node = instantiate(prefab) as Node;
            GuideManager.nodes.push(node);
            node.setParent(find("Canvas"));

            let positions = [];
            lines.forEach(e => { positions.push(); });
            node.setPosition(positions[0]);

            let timerGap2 = Vec2.distance(positions[1], positions[2]) / Vec2.distance(positions[0], positions[1]) * timeGap;
            let timerGap3 = Vec2.distance(positions[2], positions[3]) / Vec2.distance(positions[0], positions[1]) * timeGap;
            let t = tween(node)
                .to(timeGap, { position: v3(positions[1]) })
                .to(timerGap2, { position: v3(positions[2]) })
                .to(timerGap3, { position: v3(positions[3]) })
                .call(() => { node.setPosition(positions[0]); })
                .delay(timeGap);

            tween(node).repeatForever(t).start();
        });
    }

    static Stop() {
        GuideManager.nodes.forEach(e => {
            Tween.stopAllByTarget(e);
            e.destroy()
        });
        GuideManager.nodes = [];
    }
}
